cjpais lbourdois commited on
Commit
c4ed9e7
·
verified ·
1 Parent(s): 6a5b427

Improve language tag (#1)

Browse files

- Improve language tag (34b1637faac28d387a4fb191db4ed3e842c27181)


Co-authored-by: Loïck BOURDOIS <lbourdois@users.noreply.huggingface.co>

Files changed (1) hide show
  1. README.md +357 -345
README.md CHANGED
@@ -1,345 +1,357 @@
1
- ---
2
- license: apache-2.0
3
- pipeline_tag: text-generation
4
- language:
5
- - en
6
- license_link: LICENSE
7
- base_model: Qwen/Qwen2.5-14B
8
- quantized_by: bartowski
9
- tags:
10
- - llamafile
11
- - chat
12
- ---
13
-
14
- # Qwen 2.5 Instruct 14B - llamafile
15
-
16
- - Model creator: [Qwen](https://huggingface.co/Qwen/)
17
- - Original model: [Qwen/Qwen2.5-14B-Instruct-1M](https://huggingface.co/Qwen/Qwen2.5-14B-Instruct-1M/)
18
-
19
- Mozilla packaged the Qwen 2.5 models into executable weights that we
20
- call [llamafiles](https://github.com/Mozilla-Ocho/llamafile). This gives
21
- you the easiest fastest way to use the model on Linux, MacOS, Windows,
22
- FreeBSD, OpenBSD and NetBSD systems you control on both AMD64 and ARM64.
23
-
24
- *Software Last Updated: 2025-03-31*
25
-
26
- *Llamafile Version: 0.9.2*
27
-
28
- ## Quickstart
29
-
30
- To get started, you need both the Qwen 2.5 weights, and the llamafile
31
- software. Both of them are included in a single file, which can be
32
- downloaded and run as follows:
33
-
34
- ```
35
- wget https://huggingface.co/Mozilla/Qwen2.5-14B-Instruct-1M-llamafile/resolve/main/Qwen2.5-14B-Instruct-1M-Q6_K.llamafile
36
- chmod +x Qwen2.5-14B-Instruct-1M-Q6_K.llamafile
37
- ./Qwen2.5-14B-Instruct-1M-Q6_K.llamafile
38
- ```
39
-
40
- The default mode of operation for these llamafiles is our new command
41
- line chatbot interface.
42
-
43
- ## Usage
44
-
45
- You can use triple quotes to ask questions on multiple lines. You can
46
- pass commands like `/stats` and `/context` to see runtime status
47
- information. You can change the system prompt by passing the `-p "new
48
- system prompt"` flag. You can press CTRL-C to interrupt the model.
49
- Finally CTRL-D may be used to exit.
50
-
51
- If you prefer to use a web GUI, then a `--server` mode is provided, that
52
- will open a tab with a chatbot and completion interface in your browser.
53
- For additional help on how it may be used, pass the `--help` flag. The
54
- server also has an OpenAI API compatible completions endpoint that can
55
- be accessed via Python using the `openai` pip package.
56
-
57
- ```
58
- ./Qwen2.5-14B-Instruct-1M-Q6_K.llamafile --server
59
- ```
60
-
61
- An advanced CLI mode is provided that's useful for shell scripting. You
62
- can use it by passing the `--cli` flag. For additional help on how it
63
- may be used, pass the `--help` flag.
64
-
65
- ```
66
- ./Qwen2.5-14B-Instruct-1M-Q6_K.llamafile --cli -p 'four score and seven' --log-disable
67
- ```
68
-
69
- ## Troubleshooting
70
-
71
- Having **trouble?** See the ["Gotchas"
72
- section](https://github.com/mozilla-ocho/llamafile/?tab=readme-ov-file#gotchas-and-troubleshooting)
73
- of the README.
74
-
75
- On Linux, the way to avoid run-detector errors is to install the APE
76
- interpreter.
77
-
78
- ```sh
79
- sudo wget -O /usr/bin/ape https://cosmo.zip/pub/cosmos/bin/ape-$(uname -m).elf
80
- sudo chmod +x /usr/bin/ape
81
- sudo sh -c "echo ':APE:M::MZqFpD::/usr/bin/ape:' >/proc/sys/fs/binfmt_misc/register"
82
- sudo sh -c "echo ':APE-jart:M::jartsr::/usr/bin/ape:' >/proc/sys/fs/binfmt_misc/register"
83
- ```
84
-
85
- On Windows there's a 4GB limit on executable sizes.
86
-
87
- ## Context Window
88
-
89
- This model has a max context window size of 128k tokens. By default, a
90
- context window size of 8192 tokens is used. You can ask llamafile
91
- to use the maximum context size by passing the `-c 0` flag. That's big
92
- enough for a small book. If you want to be able to have a conversation
93
- with your book, you can use the `-f book.txt` flag.
94
-
95
- ## GPU Acceleration
96
-
97
- On GPUs with sufficient RAM, the `-ngl 999` flag may be passed to use
98
- the system's NVIDIA or AMD GPU(s). On Windows, only the graphics card
99
- driver needs to be installed if you own an NVIDIA GPU. On Windows, if
100
- you have an AMD GPU, you should install the ROCm SDK v6.1 and then pass
101
- the flags `--recompile --gpu amd` the first time you run your llamafile.
102
-
103
- On NVIDIA GPUs, by default, the prebuilt tinyBLAS library is used to
104
- perform matrix multiplications. This is open source software, but it
105
- doesn't go as fast as closed source cuBLAS. If you have the CUDA SDK
106
- installed on your system, then you can pass the `--recompile` flag to
107
- build a GGML CUDA library just for your system that uses cuBLAS. This
108
- ensures you get maximum performance.
109
-
110
- For further information, please see the [llamafile
111
- README](https://github.com/mozilla-ocho/llamafile/).
112
-
113
- ## About llamafile
114
-
115
- llamafile is a new format introduced by Mozilla on Nov 20th 2023. It
116
- uses Cosmopolitan Libc to turn LLM weights into runnable llama.cpp
117
- binaries that run on the stock installs of six OSes for both ARM64 and
118
- AMD64.
119
-
120
- ---
121
-
122
- # Qwen2.5-14B-Instruct-1M
123
- <a href="https://chat.qwenlm.ai/" target="_blank" style="margin: 2px;">
124
- <img alt="Chat" src="https://img.shields.io/badge/%F0%9F%92%9C%EF%B8%8F%20Qwen%20Chat%20-536af5" style="display: inline-block; vertical-align: middle;"/>
125
- </a>
126
-
127
- ## Introduction
128
-
129
- Qwen2.5-1M is the long-context version of the Qwen2.5 series models, supporting a context length of up to 1M tokens. Compared to the Qwen2.5 128K version, Qwen2.5-1M demonstrates significantly improved performance in handling long-context tasks while maintaining its capability in short tasks.
130
-
131
- The model has the following features:
132
- - Type: Causal Language Models
133
- - Training Stage: Pretraining & Post-training
134
- - Architecture: transformers with RoPE, SwiGLU, RMSNorm, and Attention QKV bias
135
- - Number of Parameters: 14.7B
136
- - Number of Paramaters (Non-Embedding): 13.1B
137
- - Number of Layers: 48
138
- - Number of Attention Heads (GQA): 40 for Q and 8 for KV
139
- - Context Length: Full 1,010,000 tokens and generation 8192 tokens
140
- - We recommend deploying with our custom vLLM, which introduces sparse attention and length extrapolation methods to ensure efficiency and accuracy for long-context tasks. For specific guidance, refer to [this section](#processing-ultra-long-texts).
141
- - You can also use the previous framework that supports Qwen2.5 for inference, but accuracy degradation may occur for sequences exceeding 262,144 tokens.
142
-
143
- For more details, please refer to our [blog](https://qwenlm.github.io/blog/qwen2.5-1m/), [GitHub](https://github.com/QwenLM/Qwen2.5), [Technical Report](https://huggingface.co/papers/2501.15383), and [Documentation](https://qwen.readthedocs.io/en/latest/).
144
-
145
- ## Requirements
146
-
147
- The code of Qwen2.5 has been in the latest Hugging face `transformers` and we advise you to use the latest version of `transformers`.
148
-
149
- With `transformers<4.37.0`, you will encounter the following error:
150
- ```
151
- KeyError: 'qwen2'
152
- ```
153
-
154
- ## Quickstart
155
-
156
- Here provides a code snippet with `apply_chat_template` to show you how to load the tokenizer and model and how to generate contents.
157
-
158
- ```python
159
- from transformers import AutoModelForCausalLM, AutoTokenizer
160
-
161
- model_name = "Qwen/Qwen2.5-14B-Instruct-1M"
162
-
163
- model = AutoModelForCausalLM.from_pretrained(
164
- model_name,
165
- torch_dtype="auto",
166
- device_map="auto"
167
- )
168
- tokenizer = AutoTokenizer.from_pretrained(model_name)
169
-
170
- prompt = "Give me a short introduction to large language model."
171
- messages = [
172
- {"role": "system", "content": "You are Qwen, created by Alibaba Cloud. You are a helpful assistant."},
173
- {"role": "user", "content": prompt}
174
- ]
175
- text = tokenizer.apply_chat_template(
176
- messages,
177
- tokenize=False,
178
- add_generation_prompt=True
179
- )
180
- model_inputs = tokenizer([text], return_tensors="pt").to(model.device)
181
-
182
- generated_ids = model.generate(
183
- **model_inputs,
184
- max_new_tokens=512
185
- )
186
- generated_ids = [
187
- output_ids[len(input_ids):] for input_ids, output_ids in zip(model_inputs.input_ids, generated_ids)
188
- ]
189
-
190
- response = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
191
- ```
192
-
193
- ### Processing Ultra Long Texts
194
-
195
- To enhance processing accuracy and efficiency for long sequences, we have developed an advanced inference framework based on vLLM, incorporating sparse attention and length extrapolation. This approach significantly improves model generation performance for sequences exceeding 256K tokens and achieves a 3 to 7 times speedup for sequences up to 1M tokens.
196
-
197
- Here we provide step-by-step instructions for deploying the Qwen2.5-1M models with our framework.
198
-
199
- #### 1. System Preparation
200
-
201
- To achieve the best performance, we recommend using GPUs with Ampere or Hopper architecture, which support optimized kernels.
202
-
203
- Ensure your system meets the following requirements:
204
-
205
- - **CUDA Version**: 12.1 or 12.3
206
- - **Python Version**: >=3.9 and <=3.12
207
-
208
- **VRAM Requirements:**
209
-
210
- - For processing 1 million-token sequences:
211
- - **Qwen2.5-7B-Instruct-1M**: At least 120GB VRAM (total across GPUs).
212
- - **Qwen2.5-14B-Instruct-1M**: At least 320GB VRAM (total across GPUs).
213
-
214
- If your GPUs do not have sufficient VRAM, you can still use Qwen2.5-1M for shorter tasks.
215
-
216
- #### 2. Install Dependencies
217
-
218
- For now, you need to clone the vLLM repository from our custom branch and install it manually. We are working on getting our branch merged into the main vLLM project.
219
-
220
- ```bash
221
- git clone -b dev/dual-chunk-attn git@github.com:QwenLM/vllm.git
222
- cd vllm
223
- pip install -e . -v
224
- ```
225
-
226
-
227
- #### 3. Launch vLLM
228
-
229
- vLLM supports offline inference or launch an openai-like server.
230
-
231
- **Example of Offline Inference**
232
-
233
- ```python
234
- from transformers import AutoTokenizer
235
- from vllm import LLM, SamplingParams
236
-
237
- # Initialize the tokenizer
238
- tokenizer = AutoTokenizer.from_pretrained("Qwen/Qwen2.5-14B-Instruct-1M")
239
-
240
- # Pass the default decoding hyperparameters of Qwen2.5-14B-Instruct
241
- # max_tokens is for the maximum length for generation.
242
- sampling_params = SamplingParams(temperature=0.7, top_p=0.8, repetition_penalty=1.05, max_tokens=512)
243
-
244
- # Input the model name or path. See below for parameter explanation (after the example of openai-like server).
245
- llm = LLM(model="Qwen/Qwen2.5-14B-Instruct-1M",
246
- tensor_parallel_size=4,
247
- max_model_len=1010000,
248
- enable_chunked_prefill=True,
249
- max_num_batched_tokens=131072,
250
- enforce_eager=True,
251
- # quantization="fp8", # Enabling FP8 quantization for model weights can reduce memory usage.
252
- )
253
-
254
- # Prepare your prompts
255
- prompt = "Tell me something about large language models."
256
- messages = [
257
- {"role": "system", "content": "You are Qwen, created by Alibaba Cloud. You are a helpful assistant."},
258
- {"role": "user", "content": prompt}
259
- ]
260
- text = tokenizer.apply_chat_template(
261
- messages,
262
- tokenize=False,
263
- add_generation_prompt=True
264
- )
265
-
266
- # generate outputs
267
- outputs = llm.generate([text], sampling_params)
268
-
269
- # Print the outputs.
270
- for output in outputs:
271
- prompt = output.prompt
272
- generated_text = output.outputs[0].text
273
- print(f"Prompt: {prompt!r}, Generated text: {generated_text!r}")
274
- ```
275
-
276
- **Example of Openai-like Server**
277
-
278
- ```bash
279
- vllm serve Qwen/Qwen2.5-14B-Instruct-1M \
280
- --tensor-parallel-size 4 \
281
- --max-model-len 1010000 \
282
- --enable-chunked-prefill --max-num-batched-tokens 131072 \
283
- --enforce-eager \
284
- --max-num-seqs 1
285
-
286
- # --quantization fp8 # Enabling FP8 quantization for model weights can reduce memory usage.
287
- ```
288
-
289
- Then you can use curl or python to interact with the deployed model.
290
-
291
- **Parameter Explanations:**
292
-
293
- - **`--tensor-parallel-size`**
294
- - Set to the number of GPUs you are using. Max 4 GPUs for the 7B model, and 8 GPUs for the 14B model.
295
-
296
- - **`--max-model-len`**
297
- - Defines the maximum input sequence length. Reduce this value if you encounter Out of Memory issues.
298
-
299
- - **`--max-num-batched-tokens`**
300
- - Sets the chunk size in Chunked Prefill. A smaller value reduces activation memory usage but may slow down inference.
301
- - Recommend 131072 for optimal performance.
302
-
303
- - **`--max-num-seqs`**
304
- - Limits concurrent sequences processed.
305
-
306
- You can also refer to our [Documentation](https://qwen.readthedocs.io/en/latest/deployment/vllm.html) for usage of vLLM.
307
-
308
- #### Troubleshooting:
309
-
310
- 1. Encountering the error: "The model's max sequence length (xxxxx) is larger than the maximum number of tokens that can be stored in the KV cache."
311
-
312
- The VRAM reserved for the KV cache is insufficient. Consider reducing the ``max_model_len`` or increasing the ``tensor_parallel_size``. Alternatively, you can reduce ``max_num_batched_tokens``, although this may significantly slow down inference.
313
-
314
- 2. Encountering the error: "torch.OutOfMemoryError: CUDA out of memory."
315
-
316
- The VRAM reserved for activation weights is insufficient. You can try setting ``gpu_memory_utilization`` to 0.85 or lower, but be aware that this might reduce the VRAM available for the KV cache.
317
-
318
- 3. Encountering the error: "Input prompt (xxxxx tokens) + lookahead slots (0) is too long and exceeds the capacity of the block manager."
319
-
320
- The input is too lengthy. Consider using a shorter sequence or increasing the ``max_model_len``.
321
-
322
- ## Evaluation & Performance
323
-
324
- Detailed evaluation results are reported in this [📑 blog](https://qwenlm.github.io/blog/qwen2.5-1m/) and our [technical report](https://arxiv.org/abs/2501.15383).
325
-
326
- ## Citation
327
-
328
- If you find our work helpful, feel free to give us a cite.
329
-
330
- ```
331
- @misc{qwen2.5-1m,
332
- title = {Qwen2.5-1M: Deploy Your Own Qwen with Context Length up to 1M Tokens},
333
- url = {https://qwenlm.github.io/blog/qwen2.5-1m/},
334
- author = {Qwen Team},
335
- month = {January},
336
- year = {2025}
337
- }
338
-
339
- @article{qwen2.5,
340
- title={Qwen2.5-1M Technical Report},
341
- author={An Yang and Bowen Yu and Chengyuan Li and Dayiheng Liu and Fei Huang and Haoyan Huang and Jiandong Jiang and Jianhong Tu and Jianwei Zhang and Jingren Zhou and Junyang Lin and Kai Dang and Kexin Yang and Le Yu and Mei Li and Minmin Sun and Qin Zhu and Rui Men and Tao He and Weijia Xu and Wenbiao Yin and Wenyuan Yu and Xiafei Qiu and Xingzhang Ren and Xinlong Yang and Yong Li and Zhiying Xu and Zipeng Zhang},
342
- journal={arXiv preprint arXiv:2501.15383},
343
- year={2025}
344
- }
345
- ```
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: apache-2.0
3
+ pipeline_tag: text-generation
4
+ language:
5
+ - zho
6
+ - eng
7
+ - fra
8
+ - spa
9
+ - por
10
+ - deu
11
+ - ita
12
+ - rus
13
+ - jpn
14
+ - kor
15
+ - vie
16
+ - tha
17
+ - ara
18
+ license_link: LICENSE
19
+ base_model: Qwen/Qwen2.5-14B
20
+ quantized_by: bartowski
21
+ tags:
22
+ - llamafile
23
+ - chat
24
+ ---
25
+
26
+ # Qwen 2.5 Instruct 14B - llamafile
27
+
28
+ - Model creator: [Qwen](https://huggingface.co/Qwen/)
29
+ - Original model: [Qwen/Qwen2.5-14B-Instruct-1M](https://huggingface.co/Qwen/Qwen2.5-14B-Instruct-1M/)
30
+
31
+ Mozilla packaged the Qwen 2.5 models into executable weights that we
32
+ call [llamafiles](https://github.com/Mozilla-Ocho/llamafile). This gives
33
+ you the easiest fastest way to use the model on Linux, MacOS, Windows,
34
+ FreeBSD, OpenBSD and NetBSD systems you control on both AMD64 and ARM64.
35
+
36
+ *Software Last Updated: 2025-03-31*
37
+
38
+ *Llamafile Version: 0.9.2*
39
+
40
+ ## Quickstart
41
+
42
+ To get started, you need both the Qwen 2.5 weights, and the llamafile
43
+ software. Both of them are included in a single file, which can be
44
+ downloaded and run as follows:
45
+
46
+ ```
47
+ wget https://huggingface.co/Mozilla/Qwen2.5-14B-Instruct-1M-llamafile/resolve/main/Qwen2.5-14B-Instruct-1M-Q6_K.llamafile
48
+ chmod +x Qwen2.5-14B-Instruct-1M-Q6_K.llamafile
49
+ ./Qwen2.5-14B-Instruct-1M-Q6_K.llamafile
50
+ ```
51
+
52
+ The default mode of operation for these llamafiles is our new command
53
+ line chatbot interface.
54
+
55
+ ## Usage
56
+
57
+ You can use triple quotes to ask questions on multiple lines. You can
58
+ pass commands like `/stats` and `/context` to see runtime status
59
+ information. You can change the system prompt by passing the `-p "new
60
+ system prompt"` flag. You can press CTRL-C to interrupt the model.
61
+ Finally CTRL-D may be used to exit.
62
+
63
+ If you prefer to use a web GUI, then a `--server` mode is provided, that
64
+ will open a tab with a chatbot and completion interface in your browser.
65
+ For additional help on how it may be used, pass the `--help` flag. The
66
+ server also has an OpenAI API compatible completions endpoint that can
67
+ be accessed via Python using the `openai` pip package.
68
+
69
+ ```
70
+ ./Qwen2.5-14B-Instruct-1M-Q6_K.llamafile --server
71
+ ```
72
+
73
+ An advanced CLI mode is provided that's useful for shell scripting. You
74
+ can use it by passing the `--cli` flag. For additional help on how it
75
+ may be used, pass the `--help` flag.
76
+
77
+ ```
78
+ ./Qwen2.5-14B-Instruct-1M-Q6_K.llamafile --cli -p 'four score and seven' --log-disable
79
+ ```
80
+
81
+ ## Troubleshooting
82
+
83
+ Having **trouble?** See the ["Gotchas"
84
+ section](https://github.com/mozilla-ocho/llamafile/?tab=readme-ov-file#gotchas-and-troubleshooting)
85
+ of the README.
86
+
87
+ On Linux, the way to avoid run-detector errors is to install the APE
88
+ interpreter.
89
+
90
+ ```sh
91
+ sudo wget -O /usr/bin/ape https://cosmo.zip/pub/cosmos/bin/ape-$(uname -m).elf
92
+ sudo chmod +x /usr/bin/ape
93
+ sudo sh -c "echo ':APE:M::MZqFpD::/usr/bin/ape:' >/proc/sys/fs/binfmt_misc/register"
94
+ sudo sh -c "echo ':APE-jart:M::jartsr::/usr/bin/ape:' >/proc/sys/fs/binfmt_misc/register"
95
+ ```
96
+
97
+ On Windows there's a 4GB limit on executable sizes.
98
+
99
+ ## Context Window
100
+
101
+ This model has a max context window size of 128k tokens. By default, a
102
+ context window size of 8192 tokens is used. You can ask llamafile
103
+ to use the maximum context size by passing the `-c 0` flag. That's big
104
+ enough for a small book. If you want to be able to have a conversation
105
+ with your book, you can use the `-f book.txt` flag.
106
+
107
+ ## GPU Acceleration
108
+
109
+ On GPUs with sufficient RAM, the `-ngl 999` flag may be passed to use
110
+ the system's NVIDIA or AMD GPU(s). On Windows, only the graphics card
111
+ driver needs to be installed if you own an NVIDIA GPU. On Windows, if
112
+ you have an AMD GPU, you should install the ROCm SDK v6.1 and then pass
113
+ the flags `--recompile --gpu amd` the first time you run your llamafile.
114
+
115
+ On NVIDIA GPUs, by default, the prebuilt tinyBLAS library is used to
116
+ perform matrix multiplications. This is open source software, but it
117
+ doesn't go as fast as closed source cuBLAS. If you have the CUDA SDK
118
+ installed on your system, then you can pass the `--recompile` flag to
119
+ build a GGML CUDA library just for your system that uses cuBLAS. This
120
+ ensures you get maximum performance.
121
+
122
+ For further information, please see the [llamafile
123
+ README](https://github.com/mozilla-ocho/llamafile/).
124
+
125
+ ## About llamafile
126
+
127
+ llamafile is a new format introduced by Mozilla on Nov 20th 2023. It
128
+ uses Cosmopolitan Libc to turn LLM weights into runnable llama.cpp
129
+ binaries that run on the stock installs of six OSes for both ARM64 and
130
+ AMD64.
131
+
132
+ ---
133
+
134
+ # Qwen2.5-14B-Instruct-1M
135
+ <a href="https://chat.qwenlm.ai/" target="_blank" style="margin: 2px;">
136
+ <img alt="Chat" src="https://img.shields.io/badge/%F0%9F%92%9C%EF%B8%8F%20Qwen%20Chat%20-536af5" style="display: inline-block; vertical-align: middle;"/>
137
+ </a>
138
+
139
+ ## Introduction
140
+
141
+ Qwen2.5-1M is the long-context version of the Qwen2.5 series models, supporting a context length of up to 1M tokens. Compared to the Qwen2.5 128K version, Qwen2.5-1M demonstrates significantly improved performance in handling long-context tasks while maintaining its capability in short tasks.
142
+
143
+ The model has the following features:
144
+ - Type: Causal Language Models
145
+ - Training Stage: Pretraining & Post-training
146
+ - Architecture: transformers with RoPE, SwiGLU, RMSNorm, and Attention QKV bias
147
+ - Number of Parameters: 14.7B
148
+ - Number of Paramaters (Non-Embedding): 13.1B
149
+ - Number of Layers: 48
150
+ - Number of Attention Heads (GQA): 40 for Q and 8 for KV
151
+ - Context Length: Full 1,010,000 tokens and generation 8192 tokens
152
+ - We recommend deploying with our custom vLLM, which introduces sparse attention and length extrapolation methods to ensure efficiency and accuracy for long-context tasks. For specific guidance, refer to [this section](#processing-ultra-long-texts).
153
+ - You can also use the previous framework that supports Qwen2.5 for inference, but accuracy degradation may occur for sequences exceeding 262,144 tokens.
154
+
155
+ For more details, please refer to our [blog](https://qwenlm.github.io/blog/qwen2.5-1m/), [GitHub](https://github.com/QwenLM/Qwen2.5), [Technical Report](https://huggingface.co/papers/2501.15383), and [Documentation](https://qwen.readthedocs.io/en/latest/).
156
+
157
+ ## Requirements
158
+
159
+ The code of Qwen2.5 has been in the latest Hugging face `transformers` and we advise you to use the latest version of `transformers`.
160
+
161
+ With `transformers<4.37.0`, you will encounter the following error:
162
+ ```
163
+ KeyError: 'qwen2'
164
+ ```
165
+
166
+ ## Quickstart
167
+
168
+ Here provides a code snippet with `apply_chat_template` to show you how to load the tokenizer and model and how to generate contents.
169
+
170
+ ```python
171
+ from transformers import AutoModelForCausalLM, AutoTokenizer
172
+
173
+ model_name = "Qwen/Qwen2.5-14B-Instruct-1M"
174
+
175
+ model = AutoModelForCausalLM.from_pretrained(
176
+ model_name,
177
+ torch_dtype="auto",
178
+ device_map="auto"
179
+ )
180
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
181
+
182
+ prompt = "Give me a short introduction to large language model."
183
+ messages = [
184
+ {"role": "system", "content": "You are Qwen, created by Alibaba Cloud. You are a helpful assistant."},
185
+ {"role": "user", "content": prompt}
186
+ ]
187
+ text = tokenizer.apply_chat_template(
188
+ messages,
189
+ tokenize=False,
190
+ add_generation_prompt=True
191
+ )
192
+ model_inputs = tokenizer([text], return_tensors="pt").to(model.device)
193
+
194
+ generated_ids = model.generate(
195
+ **model_inputs,
196
+ max_new_tokens=512
197
+ )
198
+ generated_ids = [
199
+ output_ids[len(input_ids):] for input_ids, output_ids in zip(model_inputs.input_ids, generated_ids)
200
+ ]
201
+
202
+ response = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
203
+ ```
204
+
205
+ ### Processing Ultra Long Texts
206
+
207
+ To enhance processing accuracy and efficiency for long sequences, we have developed an advanced inference framework based on vLLM, incorporating sparse attention and length extrapolation. This approach significantly improves model generation performance for sequences exceeding 256K tokens and achieves a 3 to 7 times speedup for sequences up to 1M tokens.
208
+
209
+ Here we provide step-by-step instructions for deploying the Qwen2.5-1M models with our framework.
210
+
211
+ #### 1. System Preparation
212
+
213
+ To achieve the best performance, we recommend using GPUs with Ampere or Hopper architecture, which support optimized kernels.
214
+
215
+ Ensure your system meets the following requirements:
216
+
217
+ - **CUDA Version**: 12.1 or 12.3
218
+ - **Python Version**: >=3.9 and <=3.12
219
+
220
+ **VRAM Requirements:**
221
+
222
+ - For processing 1 million-token sequences:
223
+ - **Qwen2.5-7B-Instruct-1M**: At least 120GB VRAM (total across GPUs).
224
+ - **Qwen2.5-14B-Instruct-1M**: At least 320GB VRAM (total across GPUs).
225
+
226
+ If your GPUs do not have sufficient VRAM, you can still use Qwen2.5-1M for shorter tasks.
227
+
228
+ #### 2. Install Dependencies
229
+
230
+ For now, you need to clone the vLLM repository from our custom branch and install it manually. We are working on getting our branch merged into the main vLLM project.
231
+
232
+ ```bash
233
+ git clone -b dev/dual-chunk-attn git@github.com:QwenLM/vllm.git
234
+ cd vllm
235
+ pip install -e . -v
236
+ ```
237
+
238
+
239
+ #### 3. Launch vLLM
240
+
241
+ vLLM supports offline inference or launch an openai-like server.
242
+
243
+ **Example of Offline Inference**
244
+
245
+ ```python
246
+ from transformers import AutoTokenizer
247
+ from vllm import LLM, SamplingParams
248
+
249
+ # Initialize the tokenizer
250
+ tokenizer = AutoTokenizer.from_pretrained("Qwen/Qwen2.5-14B-Instruct-1M")
251
+
252
+ # Pass the default decoding hyperparameters of Qwen2.5-14B-Instruct
253
+ # max_tokens is for the maximum length for generation.
254
+ sampling_params = SamplingParams(temperature=0.7, top_p=0.8, repetition_penalty=1.05, max_tokens=512)
255
+
256
+ # Input the model name or path. See below for parameter explanation (after the example of openai-like server).
257
+ llm = LLM(model="Qwen/Qwen2.5-14B-Instruct-1M",
258
+ tensor_parallel_size=4,
259
+ max_model_len=1010000,
260
+ enable_chunked_prefill=True,
261
+ max_num_batched_tokens=131072,
262
+ enforce_eager=True,
263
+ # quantization="fp8", # Enabling FP8 quantization for model weights can reduce memory usage.
264
+ )
265
+
266
+ # Prepare your prompts
267
+ prompt = "Tell me something about large language models."
268
+ messages = [
269
+ {"role": "system", "content": "You are Qwen, created by Alibaba Cloud. You are a helpful assistant."},
270
+ {"role": "user", "content": prompt}
271
+ ]
272
+ text = tokenizer.apply_chat_template(
273
+ messages,
274
+ tokenize=False,
275
+ add_generation_prompt=True
276
+ )
277
+
278
+ # generate outputs
279
+ outputs = llm.generate([text], sampling_params)
280
+
281
+ # Print the outputs.
282
+ for output in outputs:
283
+ prompt = output.prompt
284
+ generated_text = output.outputs[0].text
285
+ print(f"Prompt: {prompt!r}, Generated text: {generated_text!r}")
286
+ ```
287
+
288
+ **Example of Openai-like Server**
289
+
290
+ ```bash
291
+ vllm serve Qwen/Qwen2.5-14B-Instruct-1M \
292
+ --tensor-parallel-size 4 \
293
+ --max-model-len 1010000 \
294
+ --enable-chunked-prefill --max-num-batched-tokens 131072 \
295
+ --enforce-eager \
296
+ --max-num-seqs 1
297
+
298
+ # --quantization fp8 # Enabling FP8 quantization for model weights can reduce memory usage.
299
+ ```
300
+
301
+ Then you can use curl or python to interact with the deployed model.
302
+
303
+ **Parameter Explanations:**
304
+
305
+ - **`--tensor-parallel-size`**
306
+ - Set to the number of GPUs you are using. Max 4 GPUs for the 7B model, and 8 GPUs for the 14B model.
307
+
308
+ - **`--max-model-len`**
309
+ - Defines the maximum input sequence length. Reduce this value if you encounter Out of Memory issues.
310
+
311
+ - **`--max-num-batched-tokens`**
312
+ - Sets the chunk size in Chunked Prefill. A smaller value reduces activation memory usage but may slow down inference.
313
+ - Recommend 131072 for optimal performance.
314
+
315
+ - **`--max-num-seqs`**
316
+ - Limits concurrent sequences processed.
317
+
318
+ You can also refer to our [Documentation](https://qwen.readthedocs.io/en/latest/deployment/vllm.html) for usage of vLLM.
319
+
320
+ #### Troubleshooting:
321
+
322
+ 1. Encountering the error: "The model's max sequence length (xxxxx) is larger than the maximum number of tokens that can be stored in the KV cache."
323
+
324
+ The VRAM reserved for the KV cache is insufficient. Consider reducing the ``max_model_len`` or increasing the ``tensor_parallel_size``. Alternatively, you can reduce ``max_num_batched_tokens``, although this may significantly slow down inference.
325
+
326
+ 2. Encountering the error: "torch.OutOfMemoryError: CUDA out of memory."
327
+
328
+ The VRAM reserved for activation weights is insufficient. You can try setting ``gpu_memory_utilization`` to 0.85 or lower, but be aware that this might reduce the VRAM available for the KV cache.
329
+
330
+ 3. Encountering the error: "Input prompt (xxxxx tokens) + lookahead slots (0) is too long and exceeds the capacity of the block manager."
331
+
332
+ The input is too lengthy. Consider using a shorter sequence or increasing the ``max_model_len``.
333
+
334
+ ## Evaluation & Performance
335
+
336
+ Detailed evaluation results are reported in this [📑 blog](https://qwenlm.github.io/blog/qwen2.5-1m/) and our [technical report](https://arxiv.org/abs/2501.15383).
337
+
338
+ ## Citation
339
+
340
+ If you find our work helpful, feel free to give us a cite.
341
+
342
+ ```
343
+ @misc{qwen2.5-1m,
344
+ title = {Qwen2.5-1M: Deploy Your Own Qwen with Context Length up to 1M Tokens},
345
+ url = {https://qwenlm.github.io/blog/qwen2.5-1m/},
346
+ author = {Qwen Team},
347
+ month = {January},
348
+ year = {2025}
349
+ }
350
+
351
+ @article{qwen2.5,
352
+ title={Qwen2.5-1M Technical Report},
353
+ author={An Yang and Bowen Yu and Chengyuan Li and Dayiheng Liu and Fei Huang and Haoyan Huang and Jiandong Jiang and Jianhong Tu and Jianwei Zhang and Jingren Zhou and Junyang Lin and Kai Dang and Kexin Yang and Le Yu and Mei Li and Minmin Sun and Qin Zhu and Rui Men and Tao He and Weijia Xu and Wenbiao Yin and Wenyuan Yu and Xiafei Qiu and Xingzhang Ren and Xinlong Yang and Yong Li and Zhiying Xu and Zipeng Zhang},
354
+ journal={arXiv preprint arXiv:2501.15383},
355
+ year={2025}
356
+ }
357
+ ```